package com.ns.scs.rsr.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.sentinel.LimitType;
import com.ns.core.sentinel.RateLimiter;
import com.ns.core.utils.sg.ResultOk;
import com.ns.scs.rsr.bo.RsrHotBO;
import com.ns.scs.rsr.bo.RsrHotPageBO;
import com.ns.scs.rsr.dto.RsrHotPageDto;
import com.ns.scs.rsr.entry.RsrHotEntry;
import com.ns.scs.rsr.service.IRsrHotService;
import com.ns.scs.rsr.vo.RsrHotVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 资源相关操作
 *
 * @author lw
 * @since 2024-11-15
 */
@RestController
@RequestMapping("/rsr/hot")
@RequiredArgsConstructor
@CrossOrigin
public class RsrHotController {

    private final IRsrHotService service;

    /**
     * 改
     *
     * @param bo 配置
     */
    @PostMapping("add")
    @ReqLog(module = "热门资源模块", name = "增")
    public ResultOk add(@RequestBody RsrHotBO bo) {
        RsrHotEntry entry = new RsrHotEntry();
        BeanUtils.copyProperties(bo, entry);
        entry.setSort(service.count() + 1);
        return ResultOk.getByBoolean(service.save(entry));
    }

    /**
     * 逻辑删
     *
     * @param id 参数
     */
    @GetMapping("delete")
    @ReqLog(module = "热门资源模块", name = "逻辑删", isRecordFlag = true)
    ResultOk delete(Long id) {
        return ResultOk.getByBoolean(service.removeById(id));
    }

    /**
     * 删 - 根据id列表
     */
    @PostMapping("deleteByIds")
    @ReqLog(module = "热门资源模块", name = "批量删", isRecordFlag = true)
    public ResultOk deleteByIds(@RequestBody IdsBO bo) {
        return ResultOk.getByBoolean(service.removeBatchByIds(bo.getIds()));
    }

    /**
     * 查-集合
     *
     * @param bo 参数
     */
    @PostMapping("getList")
    @ReqLog(module = "资源模块", name = "查-集合")
    @RateLimiter(time = 5, count = 15, limitType = LimitType.IP)
    public ResultOk getList(@RequestBody RsrHotPageBO bo) {
        RsrHotPageDto dto = new RsrHotPageDto();
        BeanUtils.copyProperties(bo, dto);
        Page<RsrHotVO> page = service.getList(dto);
        return ResultOk.getPageData(page.getTotal(), page.getRecords());
    }

    /**
     * 查-不分页
     *
     * @param bo 参数
     */
    @PostMapping("getSearch")
    @ReqLog(module = "资源模块", name = "查-不分页", authFlag = false)
    @RateLimiter(time = 5, count = 15, limitType = LimitType.IP)
    public ResultOk getSearch(@RequestBody RsrHotPageBO bo) {
        RsrHotPageDto dto = new RsrHotPageDto();
        BeanUtils.copyProperties(bo, dto);
        return ResultOk.getSuccessData(service.getSearch(dto));
    }
}
